package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
	"strconv"
	"time"
)

// 获取门禁通知信息
func GetSafeNoticeInfo(p map[string]interface{}) *models.SafeNotice {
	o := orm.NewOrm()
	safeNotice := &models.SafeNotice{}
	qs := o.QueryTable("SafeNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["deviceId"]; ok {
		qs = qs.Filter("device_id", p["deviceId"])
	}
	if _, ok := p["studentId"]; ok {
		qs = qs.Filter("student_id", p["studentId"])
	}
	if _, ok := p["createdDate"]; ok {
		qs = qs.Filter("created_date", p["createdDate"])
	}
	if _, ok := p["isArrived"]; ok {
		qs = qs.Filter("is_arrived", p["isArrived"])
	}
	if _, ok := p["isLeft"]; ok {
		qs = qs.Filter("is_left", p["isLeft"])
	}
	qs.One(safeNotice)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if safeNotice.Id > 0 {
			safeNotice.ArrivedShow = safeNotice.Arrived.Format("2006-01-02 15:04:05")
			safeNotice.LeftShow = safeNotice.Left.Format("2006-01-02 15:04:05")
		}
	}
	return safeNotice
}

// 添加门禁通知
func AddSafeNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	safeNotice := &models.SafeNotice{}
	device := &models.Device{}
	if _, ok := input["device_id"]; ok {
		device.Id = utils.Atoi64(input["device_id"][0])
		o.Read(device)
	}
	safeNotice.Device = device
	student := &models.Student{}
	if _, ok := input["student_id"]; ok {
		student.Id = utils.Atoi64(input["student_id"][0])
		o.Read(student)
	}
	safeNotice.Student = student
	if _, ok := input["is_arrived"]; ok {
		safeNotice.IsArrived, _ = strconv.Atoi(input["is_arrived"][0])
	}
	if _, ok := input["is_left"]; ok {
		safeNotice.IsLeft, _ = strconv.Atoi(input["is_left"][0])
	}
	if _, ok := input["arrived"]; ok {
		loc, _ := time.LoadLocation("Local")
		theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", input["arrived"][0], loc)
		safeNotice.Arrived = theTime
	}
	if _, ok := input["left"]; ok {
		loc, _ := time.LoadLocation("Local")
		theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", input["left"][0], loc)
		safeNotice.Left = theTime
	}
	return o.Insert(safeNotice)
}

// 修改门禁通知信息
func EditSafeNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("SafeNotice").Filter("id", input["id"][0]).Update(p)
}

// 获取门禁通知列表
func GetSafeNoticeList(p map[string]interface{}) (int64, []*models.SafeNotice) {
	var safeNoticeList []*models.SafeNotice
	o := orm.NewOrm()
	qs := o.QueryTable("SafeNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["deviceId"]; ok {
		qs = qs.Filter("device_id", p["deviceId"])
	}
	if _, ok := p["studentId"]; ok {
		qs = qs.Filter("student_id", p["studentId"])
	}
	if _, ok := p["studentIds"]; ok && len(p["studentIds"].([]int64)) > 0 {
		qs = qs.Filter("student_id__in", p["studentIds"])
	}
	if _, ok := p["familySn"]; ok {
		qs = qs.Filter("Student__FamilySn", p["familySn"])
	}
	if _, ok := p["studentName"]; ok {
		qs = qs.Filter("Student__RealName__contains", p["studentName"])
	}
	if _, ok := p["createdDate"]; ok {
		qs = qs.Filter("created_date", p["createdDate"])
	}
	if _, ok := p["isArrived"]; ok {
		qs = qs.Filter("is_arrived", p["isArrived"])
	}
	if _, ok := p["isLeft"]; ok {
		qs = qs.Filter("is_left", p["isLeft"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if _, isLimit := p["limit"]; isLimit {
		qs = qs.Limit(p["limit"])
	} else if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&safeNoticeList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, safeNotice := range safeNoticeList {
			safeNotice.ArrivedShow = safeNotice.Arrived.Format("2006-01-02 15:04:05")
			safeNotice.ArrivedDateShow = safeNotice.Arrived.Format("2006-1-2")
			safeNotice.ArrivedTimeShow = safeNotice.Arrived.Format("15:04")
			safeNotice.LeftShow = safeNotice.Left.Format("2006-01-02 15:04:05")
			safeNotice.LeftDateShow = safeNotice.Left.Format("2006-1-2")
			safeNotice.LeftTimeShow = safeNotice.Left.Format("15:04")
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		for _, safeNotice := range safeNoticeList {
			safeNotice.StudentName = safeNotice.Student.RealName
			safeNotice.ArrivedDateShow = safeNotice.Arrived.Format("2006-1-2")
			safeNotice.ArrivedTimeShow = safeNotice.Arrived.Format("15:04")
			safeNotice.LeftDateShow = safeNotice.Left.Format("2006-1-2")
			safeNotice.LeftTimeShow = safeNotice.Left.Format("15:04")
		}
	}
	return totalCount, safeNoticeList
}
